package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.commons.utils.Constants;
import com.tanhua.commons.utils.UserHolder;
import com.tanhua.domain.UserInfo;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.MovementApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.mongo.Comment;
import com.tanhua.mongo.Movement;
import com.tanhua.myenum.CommentType;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.vo.CommentVo;
import com.tanhua.vo.ErrorResult;
import com.tanhua.vo.PageResult;
import io.vavr.collection.Array;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CommentService {
    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private MovementApi movementApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 返回评论列表
     *
     * @param movementId
     * @param page
     * @param pagesize
     * @return
     */
    public PageResult getComments(String movementId, Integer page, Integer pagesize) {
        //1、调用API查询评论列表
        List<Comment> commentList = commentApi.getComments(movementId, CommentType.COMMENT, page, pagesize);
        //2、判断list集合是否存在
        if (commentList == null || commentList.size() == 0) {
            return new PageResult(page, pagesize, 0L, null);
        }
        //3、提取所有的用户id,调用UserInfoAPI查询用户详情
        List<Long> userIds = CollUtil.getFieldValues(commentList, "userId", Long.class);
        //4、构造vo对象
        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds, null);
        ArrayList<CommentVo> list = new ArrayList<>();
        for (Comment comment : commentList) {
            UserInfo userInfo = map.get(comment.getUserId());
            if (userInfo != null) {
                CommentVo vo = CommentVo.init(userInfo, comment);
                list.add(vo);
            }
        }
        //5、构造返回值
        return new PageResult(page, pagesize, 0L, list);
    }

    /**
     * 发布评论方法
     *
     * @param movementId
     * @param commentText
     */
    public void publish(String movementId, String commentText) {
        //获取评论人id
        Long userId = UserHolder.getUserId();

        //封装comment对象
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(movementId));
        comment.setCommentType(CommentType.COMMENT.getType());
        comment.setContent(commentText);
        //userId为评论人的id
        comment.setUserId(userId);
        comment.setCreated(System.currentTimeMillis());
        //publishUserId为被评论人即发布人Id
        //保存对象，并在apiImpl层设置被评论人id，返回总评论数
        Integer count = commentApi.save(comment);
    }

}
